package liuliu.throughwaring.control.settting;

import android.content.Context;

import net.tsz.afinal.FinalDb;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import liuliu.custom.method.Utils;
import liuliu.custom.model.Cryptography;
import liuliu.throughwaring.control.task.QueryTask;
import liuliu.throughwaring.model.TerminalModel;

/**
 * Created by Administrator on 2015/11/24.
 */
public class SettingListener {
    Context mContext;
    FinalDb mDB;
    QueryTask queryTask;
    ISettingView iSettingView;

    public SettingListener(Context context, ISettingView settingView) {
        this.mContext = context;
        this.iSettingView = settingView;
        queryTask = new QueryTask(mContext);
        mDB = FinalDb.create(mContext);
    }

    public void downLoad() {
        List<TerminalModel> list = mDB.findAll(TerminalModel.class);
        if (list.size() > 0) {
            iSettingView.downLoad(true, "成功");
        } else {
            queryTask.getTerminal(iSettingView);
        }
    }

    public void modifyPassword(String old, String newpsw) {
        List<TerminalModel> list = mDB.findAll(TerminalModel.class);
        if (list.size() > 0) {
            try {
                if (Cryptography.hashPassword(old).equals(list.get(0).getTerminalPassword())) {
                    queryTask.modifyPassword(newpsw, iSettingView);
                } else {
                    iSettingView.downLoad(false, "密码不正确");
                }
            } catch (UnsupportedEncodingException e) {
                iSettingView.downLoad(false, "未知错误");
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                iSettingView.downLoad(false, "未知错误");
            }
        }
    }

    public void getCards() {
        queryTask.getNationAlarm(iSettingView);
    }
}
